package aeolus.wind.spider.web.commons;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.SessionAttributes;

import aeolus.wind.spider.biz.constants.LoginItem;
import aeolus.wind.spider.biz.dataobject.Identity;
import aeolus.wind.spider.biz.service.interfaces.AbilityService;
import aeolus.wind.spider.biz.service.interfaces.IdentityService;
import aeolus.wind.spider.biz.service.interfaces.VoteService;

@SessionAttributes("loginItem")
public class BaseController {

    @Autowired
    protected VoteService     voteService;
    @Autowired
    protected AbilityService  abilityService;
    @Autowired
    protected IdentityService identityService;
    @Autowired
    protected HttpSession     session;

    protected String getScreen(String uri) {
        return "screen/" + uri;
    }

    /**
     * 不知道为什么会拿到ISO-8859-1的，得强转一次
     * 
     * @param keyword
     * @return
     */
    protected String getUtfString(String keyword) {
        try {
            return new String(keyword.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return keyword;
        }
    }

    protected String getEncodedString(String keyword) {
        String newKeyword;
        try {
            newKeyword = URLEncoder.encode(keyword, "utf-8");
            return newKeyword;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return keyword;
    }

    /**
     * 莫名其妙的会出现一个session级别的变量在modlemap中取不到,不过在session中是一定能取到的..
     * 
     * @param session
     * @param model
     * @return
     */
    protected boolean checkLogin() {
        LoginItem loginItem = getLoginItem();
        return loginItem == null ? false : loginItem.isLogin();
    }

    protected boolean checkIsMyLeader(String leaderName) {
        LoginItem loginItem = getLoginItem();
        if (loginItem != null && loginItem.getMyLoginName().equals(leaderName)) {
            return true;
        }
        return false;
    }

    protected boolean checkIsLeadedByMe(String subordinateName) {
        LoginItem loginItem = getLoginItem();
        Identity subordinateIdentity = identityService.findIdentity(subordinateName);
        if (loginItem != null && subordinateIdentity != null) {
            return loginItem.getMyLoginName().equals(subordinateIdentity.getLeaderName());
        }
        return false;
    }

    protected boolean checkCanLeadGroup() {
        LoginItem loginItem = getLoginItem();
        if (loginItem != null) {
            return loginItem.isAbleToLeadGroup();
        }
        return false;
    }

    protected String getLeaderName() {
        LoginItem loginItem = getLoginItem();
        if (loginItem != null) {
            return loginItem.getMyLeaderName();
        }
        return "";
    }

    protected String getLeaderGroupName() {
        LoginItem loginItem = getLoginItem();
        if (loginItem != null) {
            return loginItem.getMyLeaderGroupName();
        }
        return "";
    }

    protected String getLoginName() {
        LoginItem loginItem = getLoginItem();
        if (loginItem != null) {
            return loginItem.getMyLoginName();
        }
        return "";
    }

    protected Identity getLoginIdentity() {
        LoginItem loginItem = getLoginItem();
        if (loginItem != null) {
            return loginItem.getLoginIdentity();
        }
        return null;
    }

    protected boolean checkMyself(String userName) {
        LoginItem loginItem = getLoginItem();
        return loginItem == null ? false : loginItem.getMyLoginName().equals(userName);
    }

    private LoginItem getLoginItem() {
        if (session == null) return null;
        return (LoginItem) session.getAttribute("loginItem");
    }
}
